package com.renfei.camerapreviewdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.cgfay.uitls.utils.PermissionUtils;
import com.renfei.camerapreviewdemo.listener.OnPreviewCaptureListener;
import com.renfei.camerapreviewdemo.model.AspectRatio;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView tvCamera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvCamera = findViewById(R.id.tv_camera);
        tvCamera.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        previewCamera();
    }

    private void previewCamera() {
        if (PermissionUtils.permissionChecking(this, Manifest.permission.CAMERA)) {
            jumpCamerapage();
        } else {
            checkPermissions();
        }
    }

    private void jumpCamerapage() {
        PreviewEngine.from(this)
                .setCameraRatio(AspectRatio.Ratio_16_9)
                .showFacePoints(false)
                .showFps(false)
                .backCamera(true)
                .startPreview();
    }

    private static final int REQUEST_CODE = 0;

    private void checkPermissions() {
        PermissionUtils.requestPermissions(this, new String[]{
                Manifest.permission.CAMERA
        }, REQUEST_CODE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                jumpCamerapage();
            }
        }
    }
}
